package com.superman.supermarket.client.activity;

import java.util.HashMap;
import java.util.Map;

import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.Window;
import com.google.inject.Inject;
import com.superman.mvpframe.client.ajax.PostManAsyncCallBack;
import com.superman.mvpframe.client.mvp.IPageView;
import com.superman.mvpframe.client.mvp.IPageViewPresenter;
import com.superman.mvpframe.client.utils.Registry;
import com.superman.mvpframe.client.utils.XJsonUtil;
import com.superman.supermarket.client.event.AddrChoosePageEvent;
import com.superman.supermarket.client.mvp.ShopAbstractActivity;
import com.superman.supermarket.client.view.addrchoose.AddrChooseView;

/**
 * 地址选择
 * 
 * @author superman
 * @version 2018年8月2日下午4:47:22
 */
public class AddressChooseActivity extends ShopAbstractActivity {

	@Inject
	AddrChooseView addrChooseView;
	private String selectedAddrId;

	private void getAddrList(final String selectedAddrId) {
		postman.post("address/list", null, new PostManAsyncCallBack() {

			@Override
			public void onBusinessError(String statusCode) {
			}

			@Override
			public void onBusinessOK(String response) {
				addrChooseView.setAddrList(XJsonUtil.getList(response), selectedAddrId);
			}
		});
	}

	@Override
	public IPageView getView() {
		return addrChooseView;
	}

	@Override
	public IPageViewPresenter getViewPresenter() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onActivity(EventBus eventBus) {

		eventBus.addHandler(AddrChoosePageEvent.TYPE, new AddrChoosePageEvent.AddrChoosePageEventHandler() {

			@Override
			public void pushToEdit(Map<String, Object> addrInfo) {
				Registry.register("addrinfo", addrInfo);
				placeController.push(placeFactory.getAddressEditPlace());
			}

			@Override
			public void select(String addrId) {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("ADDR_ID", addrId);
				placeController.back(map);
			}
		});
	}

	@Override
	public void popToRefresh() {
		// TODO Auto-generated method stub

	}

	@Override
	public void popToRefresh(Object contextData) {
		Map<String, Object> map = (Map<String, Object>) contextData;
		if (map != null && map.get("ADDR_ID") != null)
			getAddrList(this.selectedAddrId);

	}

	@Override
	public void pushToRefresh() {
		this.selectedAddrId = Window.Location.getParameter("addrid");

		getAddrList(selectedAddrId);
	}
}
